package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.put;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalPutDetailBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.putInfo.PutSingleBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalPutDetailPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalPutDetailViewModel;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.functions.Consumer;

public class InHospitalPutDetailActivity extends BaseActivity<ActivityInHospitalPutDetailBinding,InHospitalPutDetailViewModel,InHospitalPutDetailPresenter> {
    private ArrayList<PutSingleBeen> currently;
    private MDialog mDialog;
    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);
        binding.tvPutDetailTitle.titleTextTv.setText("药品上架");
        binding.tvPutDetailChufanghao.setText("NO: "+presenter.DJJLID);

        binding.tvPutDetailTitle.titleBackTv.setText("");
        binding.tvPutDetailTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.tvPutDetailTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewModel.show()){
                    mDialog = CreateDialog.exitDialog(InHospitalPutDetailActivity.this, 5, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            CreateDialog.dismiss(InHospitalPutDetailActivity.this);
                            finish();
                        }
                    },"退出上架单后，需重新开始操作");
                }else {
                    finish();
                }
            }
        });

        binding.rvPutDetail.setLayoutManager(new LinearLayoutManager(this));
        binding.rvPutDetail.setPullRefreshEnabled(false);
        binding.btPutDetailStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(InHospitalPutDetailActivity.this, InHospitalPutScanActivity.class);
                intent.putExtra("putSingle",viewModel.datas);
                intent.putExtra("DJJLID",presenter.DJJLID);
                startActivityForResult(intent, Contants.REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                //TODO:用户未操作完全的药单 按BACK键返回  刷新界面数据
                currently = (ArrayList<PutSingleBeen>) data.getExtras().getSerializable("currently");
//                Log.e("TAG", "onActivityResult: "+currently.size()  + "  "+currently.toString() );
                if(null != currently ){
                    viewModel.setWData(currently);
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        RxBus.getInstance().tObservable(Contants.INHOSPITAL_PUT_MEDICINE_DETAIL_RV_ITEM_COUNT, List.class).subscribe(new Consumer<List<PutSingleBeen>>() {
            @Override
            public void accept(List<PutSingleBeen> putSingleBeens) throws Exception {
                //TODO:获取RecycleView的总条目数
                binding.tvPutDetailTotalNum.setText("共"+putSingleBeens.size()+"种药品");
                binding.tvPutRoom.setText(putSingleBeens.get(0).getRKFSMC());
                binding.tvPutDetailTime.setText("时间："+putSingleBeens.get(0).getDJFSRQ());

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(viewModel.show()){
                mDialog = CreateDialog.exitDialog(this, 5, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CreateDialog.dismiss(InHospitalPutDetailActivity.this);
                        finish();
                    }
                },"退出上架单后，需重新开始操作");
            }
//            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != mDialog){
            mDialog.cancel();
        }

        PreferenceManager.getInstance().setHospitalPutProgressTotal("0");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_put_detail;
    }

    @Override
    protected Class<InHospitalPutDetailPresenter> getPresenterClass() {
        return InHospitalPutDetailPresenter.class;
    }

    @Override
    protected Class<InHospitalPutDetailViewModel> getViewModelClass() {
        return InHospitalPutDetailViewModel.class;
    }
}
